Squaring a Sorted Array (easy)

Problem Statement #

Given a sorted array, create a new array containing squares of all the number of the input array in the sorted order.

Example 1:

Input: [-2, -1, 0, 2, 3]
Output: [0, 1, 4, 4, 9]

Example 2:

Input: [-3, -1, 0, 1, 2]
Output: [0 1 1 4 9]

Try it yourself #

Try solving this question here:

0 of 2 Tests Passed
ResultInputExpected OutputActual OutputReason
makeSquares([-2, -1, 0, 2, 3])[0, 1, 4, 4, 9][0, 0, 0, 0, 0]Incorrect Output
makeSquares([-3, -1, 0, 1, 2])[0, 1, 1, 4, 9][0, 0, 0, 0, 0]Incorrect Output
3.734s

Solution #

This is a straightforward question. The only trick is that we can have negative numbers in the input array, which will make it a bit difficult to generate the output array with squares in sorted order.

An easier approach could be to first find the index of the first non-negative number in the array. After that, we can use Two Pointers to iterate the array. One pointer will move forward to iterate the non-negative numbers and the other pointer will move backward to iterate the negative numbers. At any step, whichever number gives us a bigger square will be added to the output array. For the above-mentioned Example-1, we will do something like this:

Created with Fabric.js 1.6.0-rc.1 -2 -1 0 2 3

Since the numbers at both the ends can give us the largest square, an alternate approach could be to use two pointers starting at both the ends of the input array. At any step, whichever pointer gives us the bigger square we add it to the result array and move to the next/previous number according to the pointer. For the above-mentioned Example-1, we will do something like this:

Created with Fabric.js 1.6.0-rc.1 -2 -1 0 2 3

Code #

Here is what our algorithm will look like:

Output

0.489s

Squares: [0, 1, 4, 4, 9] Squares: [0, 1, 1, 4, 9]

Time complexity #

The time complexity of the above algorithm will be O(N)O(N) as we are iterating the input array only once.

Space complexity #

The space complexity of the above algorithm will also be O(N)O(N); this space will be used for the output array.

Mark as Completed
←    Back
Remove Duplicates (easy)
Next    →
Triplet Sum to Zero (medium)